var form = layui.form, layer = layui.layer, id=getQueryVariable("id"),productCode=parent.code,oldCode=getQueryVariable("oldCode");

var dw=getdictionary(3,10);//单位
var cgbz=getdictionary(5,14);//采购币种
var gys=getgys();
var dwselect=$("select[name='dw']");//单位列表
dwselect.append(getoption(dw));

var cgbzselect=$("select[name='cgbz']");//采购币种
cgbzselect.append(getoption(cgbz));


var gysselect=$("select[name='gys']");//供应商
gysselect.append(getoption(gys));

form.render();

//规格尺寸中文描述
let otherSpecificationSizeChineseDes = "";
//规格尺寸英文描述
let otherSpecificationSizeEnglishDes = "";

//特殊参数中文描述
let otherSpecialParametersChineseDes = "";
//特殊参数英文描述
let otherSpecialParametersEnglishDes = "";

//品类中文描述
let otherCategoryChineseDes = "";
//品类英文描述
let otherCategoryEnglishDes = "";

//客户货号中文描述
let otherCustomerNumberChineseDes = "";
//客户货号英文描述
let otherCustomerNumberEnglishDes = "";

//特殊需求(其它)中文描述
let gasketSpecialNeedsChineseDes = "";
//特殊需求(其它)英文描述
let gasketSpecialNeedsEnglishDes = "";

//修改
function updateData() {
    //表单初始值，修改时调用
    if(id!=""){
        var oldData =  parent.table.cache["producttableid"];
        $.each(oldData, function (index, value) {
            if (oldData[index]["id"] == id) {
                form.val("editor-form",{
                    "code":oldData[index]["code"],
                    "ge":oldData[index]["ge"],
                    "chinaRemark":oldData[index]["chinaRemark"],
                    "englishRemark":oldData[index]["englishRemark"],
                    "bzsm":oldData[index]["bzsm"],
                    "cpjz":oldData[index]["cpjz"],
                    "cpmz":oldData[index]["cpmz"],
                    "colour":oldData[index]["colour"],
                    "dw":oldData[index]["dw"],
                    "cgbz":oldData[index]["cgbz"],
                    "cgcb":oldData[index]["cgcb"],
                    "fob":oldData[index]["fob"],
                    "wbzc":oldData[index]["wbzc"],
                    "wbzk":oldData[index]["wbzk"],
                    "wbzg":oldData[index]["wbzg"],
                    "wbzcc":oldData[index]["wbzcc"],
                    "wbzsl":oldData[index]["wbzsl"],
                    "tj":oldData[index]["tj"],
                    "gys":oldData[index]["gys"],
                    "cshh":oldData[index]["cshh"],
                    "hq40zl":oldData[index]["hq40zl"],
                    "gp20":oldData[index]["gp20"],
                    "gp40":oldData[index]["gp40"],
                    "lclpx":oldData[index]["lclpx"],
                    "cgcbws":oldData[index]["cgcbws"],
                    "categoryCode":oldData[index]["categoryCode"],
                    "otherSpecificationSizeName":oldData[index]["otherSpecificationSizeName"],
                    "otherSpecificationSizeCode":oldData[index]["otherSpecificationSizeCode"],
                    "otherSpecialParametersName":oldData[index]["otherSpecialParametersName"],
                    "otherSpecialParametersCode":oldData[index]["otherSpecialParametersCode"],
                    "otherCategoryName":oldData[index]["otherCategoryName"],
                    "otherCategoryCode":oldData[index]["otherCategoryCode"],
                    "otherCustomerNumberName":oldData[index]["otherCustomerNumberName"],
                    "otherCustomerNumberCode":oldData[index]["otherCustomerNumberCode"],
                    "specialNeedsCode":oldData[index]["specialNeedsCode"],
                    "specialNeedsName":oldData[index]["specialNeedsName"]

                })
                form.render('select');
                //初始化中英文描述
                //规格尺寸
                let otherSpecificationSizeList = dictionaryCategoryList["otherSpecificationSizeList"];
                for (let i in otherSpecificationSizeList) {
                    if(oldData[index]["otherSpecificationSizeCode"] === otherSpecificationSizeList[i].code){
                        //规格尺寸中文描述
                        otherSpecificationSizeChineseDes = otherSpecificationSizeList[i].remark;
                        //规格尺寸英文描述
                        otherSpecificationSizeEnglishDes = otherSpecificationSizeList[i].englishname;
                    }
                }

                //特殊参数
                let otherSpecialParametersList = dictionaryCategoryList["otherSpecialParametersList"];
                for (let i in otherSpecialParametersList) {
                    if(oldData[index]["otherSpecialParametersCode"] === otherSpecialParametersList[i].code){
                        //特殊参数中文描述
                        otherSpecialParametersChineseDes = otherSpecialParametersList[i].remark;
                        //特殊参数英文描述
                        otherSpecialParametersEnglishDes = otherSpecialParametersList[i].englishname;
                    }
                }

                //品类
                let otherCategoryList = dictionaryCategoryList["otherCategoryList"];
                for (let i in otherCategoryList) {
                    if(oldData[index]["otherCategoryCode"] === otherCategoryList[i].code){
                        //品类中文描述
                        otherCategoryChineseDes = otherCategoryList[i].remark;
                        //品类英文描述
                        otherCategoryEnglishDes = otherCategoryList[i].englishname;
                    }
                }

                //客户货号
                let otherCustomerNumberList = dictionaryCategoryList["otherCustomerNumberList"];
                for (let i in otherCustomerNumberList) {
                    if(oldData[index]["otherCustomerNumberCode"] === otherCustomerNumberList[i].code){
                        //客户货号中文描述
                        otherCustomerNumberChineseDes = otherCustomerNumberList[i].remark;
                        //客户货号英文描述
                        otherCustomerNumberEnglishDes = otherCustomerNumberList[i].englishname;
                    }
                }

                //特殊需求(其它)
                let specialNeedsList = dictionaryCategoryList["specialNeedsList"];
                for (let i in specialNeedsList) {
                    if(oldData[index]["specialNeedsCode"] === specialNeedsList[i].code){
                        //特殊需求(其它)中文描述
                        gasketSpecialNeedsChineseDes = specialNeedsList[i].remark;
                        //特殊需求(其它)英文描述
                        gasketSpecialNeedsEnglishDes = specialNeedsList[i].englishname;
                    }
                }
            }
        });
        form.render();
    }else {
        $("#wbzg").val(0);
        $("#cpjz").val(0);
        $("#cpmz").val(0);
        $("#wbzc").val(0);
        $("#wbzk").val(0);
        $("#wbzsl").val(0);
        $("#tj").val(0);
        $("#hq40zl").val(0);
        $("#gp20").val(0);
        $("#gp40").val(0);
        $("#lclpx").val(0);
        $("#cgcb").val(0);
        $("#fob").val(0);
        $("#wbzcc").val(0);
        $("#cgcbws").val(0);
        $("#cshh").val(0);

    }
}
//监听提交
form.on('submit(form-element)', function(data){
    //data.field 为表单修改或新增数据 变量ID有值的情况下是修改，否则为新增
    if(savefj(data.field)){
        layer.msg("保存成功", {icon: 6});
        parent.refrash();
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }

    return false;
});
function  savefj(data) {
    if(id !="") data.id=id;
    data.productCode=productCode;
    data.oldCode=oldCode;
    var tf=false;
    $.ajax({
        type : 'post',
        data:JSON.stringify(data),
        url:'productfjinfo/savefj',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                tf= da.data >0 ;
            }else {
                layer.msg(da.msg, {icon: 5});
            }
        }
    });
    return tf;
}
//获取所有供应商
function  getgys() {
    var gys=[];
    $.ajax({
        type : 'get',
        url:'customer2/getAllGys',
        dataType:"json",
        async:false,
        success : function (da) {
            if(da.status==200 ){
                for(var i=0;i<da.data.length;i++){
                    var obj={};
                    obj.code=da.data[i].code;
                    obj.name=da.data[i].name;
                    gys.push(obj);
                }

            }
        }
    });
    return gys;
}
function  getoption(objs) {
    var stoptions="<option value=''></option>";
    for(var i=0;i<objs.length;i++){
        stoptions+="<option value='"+objs[i].code+"'>"+objs[i].name+"</option>";
    }
    return stoptions;
}
function isNumber(val) {
    var regPos = /^\d+(\.\d+)?$/; //非负浮点数
    var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
    if (regPos.test(val) || regNeg.test(val)) {
        return true;
    } else {
        return false;
    }
}
//计算未含税出厂价
function changecgcb(){
    //含税出厂价
    let cgcb=$("#cgcb").val();
    let cgcbBig
    if (isNumber(cgcb)){
        cgcbBig=new BigNumber(cgcb);
    }else{
        cgcbBig=new BigNumber(0);
    }
    let cgcbws=cgcbBig.dividedBy(1.3).toFixed(3);
    $("#cgcbws").val(cgcbws);
    form.render();
}

//计算未含税FOB价格
function changeFob(){
    //含税出厂价
    let fob=$("#fob").val();
    let fobBig
    if (isNumber(fob)){
        fobBig=new BigNumber(fob);
    }else{
        fobBig=new BigNumber(0);
    }
    let cshh=fobBig.dividedBy(1.3).toFixed(3);
    $("#cshh").val(cshh);
    form.render();
}

//模具相关信息
let dictionaryCategoryList = [];

layer.ready(function () {
    //获取模具相关信息
    $.ajax({
        url: "product/category/getDictionaryCategory",
        async: false,
        success: function (data) {
            if (data.status == 200) {
                dictionaryCategoryList = data.data;
                //规格尺寸
                let otherSpecificationSizeList = dictionaryCategoryList["otherSpecificationSizeList"];
                for (let i in otherSpecificationSizeList) {
                    $("#otherSpecificationSizeCode").append(new Option(otherSpecificationSizeList[i].name, otherSpecificationSizeList[i].code));
                }

                //特殊参数
                let otherSpecialParametersList = dictionaryCategoryList["otherSpecialParametersList"];
                for (let i in otherSpecialParametersList) {
                    $("#otherSpecialParametersCode").append(new Option(otherSpecialParametersList[i].name, otherSpecialParametersList[i].code));
                }

                //品类
                let otherCategoryList = dictionaryCategoryList["otherCategoryList"];
                for (let i in otherCategoryList) {
                    $("#otherCategoryCode").append(new Option(otherCategoryList[i].name, otherCategoryList[i].code));
                }

                //客户货号
                let otherCustomerNumberList = dictionaryCategoryList["otherCustomerNumberList"];
                for (let i in otherCustomerNumberList) {
                    $("#otherCustomerNumberCode").append(new Option(otherCustomerNumberList[i].name, otherCustomerNumberList[i].code));
                }

                //特殊需求(其它)
                let specialNeedsList = dictionaryCategoryList["specialNeedsList"];
                for (let i in specialNeedsList) {
                    $("#specialNeedsCode").append(new Option(specialNeedsList[i].name, specialNeedsList[i].code));
                }

                form.render('select');
            }
        }
    });

    //修改
    updateData();
});


//监听规格尺寸下拉框
form.on("select(otherSpecificationSizeCode)", function(data){
    if(data.value === ""){
        $("#otherSpecificationSizeName").val("");
        return;
    }
    let otherSpecificationSizeList = dictionaryCategoryList["otherSpecificationSizeList"];
    for(let i in otherSpecificationSizeList){
        if(otherSpecificationSizeList[i].code == data.value){
            let otherSpecificationSizeName = otherSpecificationSizeList[i].name;
            otherSpecificationSizeChineseDes = otherSpecificationSizeList[i].remark;
            otherSpecificationSizeEnglishDes = otherSpecificationSizeList[i].englishname;
            $("#otherSpecificationSizeName").val(otherSpecificationSizeName);
            let newSpecCode = getNewSpecCode();
            $("#code").val(newSpecCode);
            //新的中文描述
            let newChineseDes = getNewChineseDes();
            $("#chinaRemark").val(newChineseDes);
            //新的英文描述
            let newEnglishDes = getNewEnglishDes();
            $("#englishRemark").val(newEnglishDes);
            break;
        }
    }
});

//监听特殊参数下拉框
form.on("select(otherSpecialParametersCode)", function(data){
    if(data.value === ""){
        $("#otherSpecialParametersName").val("");
        return;
    }
    let otherSpecialParametersList = dictionaryCategoryList["otherSpecialParametersList"];
    for(let i in otherSpecialParametersList){
        if(otherSpecialParametersList[i].code == data.value){
            otherSpecialParametersChineseDes = otherSpecialParametersList[i].remark;
            otherSpecialParametersEnglishDes = otherSpecialParametersList[i].englishname;
            $("#otherSpecialParametersName").val(otherSpecialParametersList[i].name);
            let newSpecCode = getNewSpecCode();
            $("#code").val(newSpecCode);
            //新的中文描述
            let newChineseDes = getNewChineseDes();
            $("#chinaRemark").val(newChineseDes);
            //新的英文描述
            let newEnglishDes = getNewEnglishDes();
            $("#englishRemark").val(newEnglishDes);
            break;
        }
    }
});

//监听品类下拉框
form.on("select(otherCategoryCode)", function(data){
    if(data.value === ""){
        $("#otherCategoryName").val("");
        return;
    }
    let otherCategoryList = dictionaryCategoryList["otherCategoryList"];
    for(let i in otherCategoryList){
        if(otherCategoryList[i].code == data.value){
            otherCategoryChineseDes = otherCategoryList[i].remark;
            otherCategoryEnglishDes = otherCategoryList[i].englishname;
            $("#otherCategoryName").val(otherCategoryList[i].name);
            let newSpecCode = getNewSpecCode();
            $("#code").val(newSpecCode);
            //新的中文描述
            let newChineseDes = getNewChineseDes();
            $("#chinaRemark").val(newChineseDes);
            //新的英文描述
            let newEnglishDes = getNewEnglishDes();
            $("#englishRemark").val(newEnglishDes);
            break;
        }
    }
});


//监听客户货号下拉框
form.on("select(otherCustomerNumberCode)", function(data){
    if(data.value === ""){
        $("#otherCustomerNumberName").val("");
        return;
    }
    let otherCustomerNumberList = dictionaryCategoryList["otherCustomerNumberList"];
    for(let i in otherCustomerNumberList){
        if(otherCustomerNumberList[i].code == data.value){
            otherCustomerNumberChineseDes = otherCustomerNumberList[i].remark;
            otherCustomerNumberEnglishDes = otherCustomerNumberList[i].englishname;
            $("#otherCustomerNumberName").val(otherCustomerNumberList[i].name);
            let newSpecCode = getNewSpecCode();
            $("#code").val(newSpecCode);
            //新的中文描述
            let newChineseDes = getNewChineseDes();
            $("#chinaRemark").val(newChineseDes);
            //新的英文描述
            let newEnglishDes = getNewEnglishDes();
            $("#englishRemark").val(newEnglishDes);
            break;
        }
    }
});

//监听特殊需求设置(其它)下拉框
form.on("select(specialNeedsCode)", function(data){
    if(data.value === ""){
        $("#specialNeedsName").val("");
        return;
    }
    let specialNeedsList = dictionaryCategoryList["specialNeedsList"];
    for(let i in specialNeedsList){
        if(specialNeedsList[i].code == data.value){
            gasketSpecialNeedsChineseDes = specialNeedsList[i].remark;
            gasketSpecialNeedsEnglishDes = specialNeedsList[i].englishname;
            $("#specialNeedsName").val(specialNeedsList[i].name);
            let newSpecCode = getNewSpecCode();
            $("#code").val(newSpecCode);
            //新的中文描述
            let newChineseDes = getNewChineseDes();
            $("#chinaRemark").val(newChineseDes);
            //新的英文描述
            let newEnglishDes = getNewEnglishDes();
            $("#englishRemark").val(newEnglishDes);
            break;
        }
    }
});


//获取新的规格编码:品类-规格尺寸-特殊参数-客户货号
function getNewSpecCode() {
    let newSpecCode = "";
    //品类
    let otherCategoryName = $("#otherCategoryName").val();
    if (otherCategoryName) {
        if(newSpecCode){
            newSpecCode = newSpecCode + "-" + otherCategoryName;
        }else {
            newSpecCode = otherCategoryName;
        }
    }
    //规格尺寸
    let otherSpecificationSizeName = $("#otherSpecificationSizeName").val();
    if (otherSpecificationSizeName) {
        if(newSpecCode){
            newSpecCode = newSpecCode + "-" + otherSpecificationSizeName;
        }else {
            newSpecCode = otherSpecificationSizeName;
        }
    }
    //特殊参数
    let otherSpecialParametersName = $("#otherSpecialParametersName").val();
    if (otherSpecialParametersName) {
        if(newSpecCode){
            newSpecCode = newSpecCode + "-" + otherSpecialParametersName;
        }else {
            newSpecCode = otherSpecialParametersName;
        }
    }
    //客户货号
    let otherCustomerNumberName = $("#otherCustomerNumberName").val();
    if (otherCustomerNumberName) {
        if(newSpecCode){
            newSpecCode = newSpecCode + "-" + otherCustomerNumberName;
        }else {
            newSpecCode = otherCustomerNumberName;
        }
    }
    //特殊需求(其它)
    let specialNeedsName = $("#specialNeedsName").val();
    if (specialNeedsName) {
        if(newSpecCode){
            newSpecCode = newSpecCode + "-" + specialNeedsName;
        }else {
            newSpecCode = specialNeedsName;
        }
    }

    return newSpecCode;
}

//获取新的中文描述:规格尺寸-特殊参数-品类-客户货号
function getNewChineseDes() {
    let newChineseDes = "";
    //规格尺寸
    if (otherSpecificationSizeChineseDes) {
        if(newChineseDes){
            newChineseDes = newChineseDes + "-" + otherSpecificationSizeChineseDes;
        }else {
            newChineseDes = otherSpecificationSizeChineseDes;
        }
    }
    //特殊参数
    if (otherSpecialParametersChineseDes) {
        if(newChineseDes){
            newChineseDes = newChineseDes + "-" + otherSpecialParametersChineseDes;
        }else {
            newChineseDes = otherSpecialParametersChineseDes;
        }
    }
    //品类
    if (otherCategoryChineseDes) {
        if(newChineseDes){
            newChineseDes = newChineseDes + "-" + otherCategoryChineseDes;
        }else {
            newChineseDes = otherCategoryChineseDes;
        }
    }
    //客户货号
    if (otherCustomerNumberChineseDes) {
        if(newChineseDes){
            newChineseDes = newChineseDes + "-" + otherCustomerNumberChineseDes;
        }else {
            newChineseDes = otherCustomerNumberChineseDes;
        }
    }
    //特殊需求(其它)
    if (gasketSpecialNeedsChineseDes) {
        if(newChineseDes){
            newChineseDes = newChineseDes + "-" + gasketSpecialNeedsChineseDes;
        }else {
            newChineseDes = gasketSpecialNeedsChineseDes;
        }
    }

    return newChineseDes;
}

//获取新的英文描述:所有英文描述以-拼接
function getNewEnglishDes() {
    let newEnglishDes = "";
    //规格尺寸
    if (otherSpecificationSizeEnglishDes) {
        if(newEnglishDes){
            newEnglishDes = newEnglishDes + "-" + otherSpecificationSizeEnglishDes;
        }else {
            newEnglishDes = otherSpecificationSizeEnglishDes;
        }
    }
    //特殊参数
    if (otherSpecialParametersEnglishDes) {
        if(newEnglishDes){
            newEnglishDes = newEnglishDes + "-" + otherSpecialParametersEnglishDes;
        }else {
            newEnglishDes = otherSpecialParametersEnglishDes;
        }
    }
    //品类
    if (otherCategoryEnglishDes) {
        if(newEnglishDes){
            newEnglishDes = newEnglishDes + "-" + otherCategoryEnglishDes;
        }else {
            newEnglishDes = otherCategoryEnglishDes;
        }
    }
    //客户货号
    if (otherCustomerNumberEnglishDes) {
        if(newEnglishDes){
            newEnglishDes = newEnglishDes + "-" + otherCustomerNumberEnglishDes;
        }else {
            newEnglishDes = otherCustomerNumberEnglishDes;
        }
    }
    //特殊需求(其它)
    if (gasketSpecialNeedsEnglishDes) {
        if(newEnglishDes){
            newEnglishDes = newEnglishDes + "-" + gasketSpecialNeedsEnglishDes;
        }else {
            newEnglishDes = gasketSpecialNeedsEnglishDes;
        }
    }

    return newEnglishDes;
}